package com.edu.exam.enums;

/**
 * 数据源类型枚举
 *
 * @author System
 * @version 1.0.0
 */
public enum DataSourceType {
    REST_API("rest_api", "REST API接口"),
    FILE_UPLOAD("file_upload", "文件上传"),
    DATABASE("database", "数据库连接"),
    FTP("ftp", "FTP服务器"),
    SFTP("sftp", "SFTP服务器"),
    WEB_SERVICE("web_service", "Web服务"),
    MESSAGE_QUEUE("message_queue", "消息队列"),
    CLOUD_STORAGE("cloud_storage", "云存储");

    private final String code;
    private final String description;

    DataSourceType(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据代码获取枚举值
     *
     * @param code 代码
     * @return 枚举值
     * @throws IllegalArgumentException 如果代码无效
     */
    public static DataSourceType fromCode(String code) {
        if (code == null) {
            throw new IllegalArgumentException("代码不能为空");
        }
        for (DataSourceType type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的数据源类型代码: " + code);
    }

    /**
     * 根据描述获取枚举值
     *
     * @param description 描述
     * @return 枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static DataSourceType fromDescription(String description) {
        if (description == null) {
            throw new IllegalArgumentException("描述不能为空");
        }
        for (DataSourceType type : values()) {
            if (type.getDescription().equals(description)) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的数据源类型描述: " + description);
    }

    @Override
    public String toString() {
        return description;
    }
}